﻿using FolderBrowser;
using SuperKit.WindowForm.GalleryWindow;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SuperKit
{
    public class GalleryFileViewModel : FileViewModelBase {
        private string _path;
        public string FullName {
            get { return _path; }
            set {
                _path = value;
                Name = Path.GetFileName(_path);
                if (IsDirectory) {
                    Size = String.Empty;
                } else {
                    Size = Media.FormatSize(new FileInfo(_path).Length);
                }                
                OnPropertyChanged("Path");
            }
        }

        private string _name;
        public string Name {
            get { return _name; }
            private set {
                _name = value;
                OnPropertyChanged("Name");
            }
        }

        private string _catalog = Kit.EMPTY;
        public string Catalog {
            get => _catalog;
            set {
                _catalog = value;
                OnPropertyChanged("Catalog");
            }
        }

        private string _size;
        public string Size {
            get => _size;
            private set {
                _size = value;
                OnPropertyChanged("Size");
            }
        }

        public bool IsDirectory { get; }
        public bool IsFile => !IsDirectory;

        public GalleryFileViewModel(string path, bool isDirectory) {
            IsDirectory = isDirectory; // 这个必须放在前面
            FullName = path;

            if (IsDirectory) {
                Type = FastCatalogItemType.Directory;
            } else {
                FileInfo info = new FileInfo(FullName);
                if (Kit.IsFileType(info, Kit.IMG_EXTS)) {
                    Type = FastCatalogItemType.Image;
                } else if (Kit.IsFileType(info, Kit.MEDIA_EXT)) {
                    Type = FastCatalogItemType.Video;
                } else {
                    Type = FastCatalogItemType.File;
                }
            }
            this.ImageCount = -1;
        }

        public static ConfigReader Cfg => ConfigReader.Instance;

        public FastCatalogItemType Type { get; }
        public GalleryManager Parent { get; }
        private int ImageCount { get; set; }


        public bool IsVideo => Type == FastCatalogItemType.Video;
        public bool IsImage => Type == FastCatalogItemType.Image;


        public int GetImageCount() {
            if (IsFile) {
                return IsImage ? 1 : 0;
            }
            if (ImageCount == -1) {
                DirectoryInfo info = new DirectoryInfo(FullName);
                ImageCount = Kit.CountAllFiles(info, Kit.IMG_EXTS);
            }
            return ImageCount;
        }


        public string GetFileName() {
            if (IsDirectory) {
                DirectoryInfo info = new DirectoryInfo(FullName);
                return info.Name;
            } else {
                FileInfo info = new FileInfo(FullName);
                return Path.GetFileNameWithoutExtension(info.Name);
            }
        }
    }
}
